package com.pinyougou.manage.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.pinyougou.pojo.TbBrand;

import com.pinyougou.sellergoods.service.BrandService;
import com.pinyougou.vo.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/brand")
public class BrandController {

    @Reference(timeout = 3000)
    private BrandService brandService;

    /**
     * 获取格式化的品牌列表；格式为：[{id:'1',text:'联想'},{id:'2',text:'华为'}]
     * @return 品牌列表；格式为：[{id:'1',text:'联想'},{id:'2',text:'华为'}]
     */
    @GetMapping("/selectOptionList")
    public List<Map<String,Object>> selectOptionList(){
        return brandService.selectOptionList();
    }

    @GetMapping(value = "/delete")
    public Result delete(Long[] ids){
        try {
            brandService.deleteByIds(ids);
            return Result.ok("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("批量删除失败");
    }

    //回显数据后的更新品牌
    @PostMapping(value = "/update")
    public Result update(@RequestBody TbBrand tbBrand){
        try {
            brandService.update(tbBrand);
            return Result.ok("更新品牌成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("更新品牌失败");
    }

    /**
     * 根据id查询一条数据回显
     * @param id
     * @return
     */
    @GetMapping(value = "/findone/{id}")
    public TbBrand findone(@PathVariable("id") Integer id){
        return brandService.findOne(id);
    }

    /**
     * 新增品牌
     * @param
     * @return 操作结果
     */
    @PostMapping(value = "/add")
    public Result add(@RequestBody TbBrand tbBrand){
        try {
            brandService.add(tbBrand);
            return Result.ok("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("保存失败");
    }

    @GetMapping(value = "/findAll")
    public List<TbBrand> queryAll(){

        //return brandService.queryAll();
        return brandService.findAll();
    }

    @GetMapping(value = "/testPage")
    public PageInfo<TbBrand> testPage(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                      @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        return brandService.findPage(pageNum,pageSize);
    }

    /**
     * 根据分页参数分页查询分页信息
     * @param pageNum 页号
     * @param pageSize 页大小
     * @return 分页信息
     */
    @GetMapping(value = "/findPage")
    public PageInfo<TbBrand> findPage(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                      @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        return brandService.findPage(pageNum,pageSize);
    }

    /**
     * 根据分页参数分页查询分页信息
     * @param pageNum 页号
     * @param pageSize 页大小
     * @return 分页信息
     */
    @PostMapping(value = "/search")
    public PageInfo<TbBrand> search(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                      @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
                                    @RequestBody TbBrand tbBrand){
        System.out.println("wo jinglai l ");
        return brandService.search(pageNum,pageSize,tbBrand);
    }
}
